package com.example.chaindemo.web.log;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.UUID;

/**
 * @author PC3
 */
public class LogbackMdcFilter implements Filter {

    private final static String REQUEST_KEY = "requestId";
    private static final Logger logger = LoggerFactory.getLogger(LogbackMdcFilter.class);

    @Override
    public void destroy() {
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response,
                         FilterChain chain) throws IOException, ServletException {
        long start = System.currentTimeMillis();
        RequestInfo reqInfo = buildRequestInfo(request);
        String uuid = UUID.randomUUID().toString();
        MDC.put(REQUEST_KEY, uuid);
        logger.info("请求信息: [{}]", reqInfo);
        chain.doFilter(request, response);
        logger.info("处理结束, 用时：[{}]ms", System.currentTimeMillis() - start);
        MDC.remove(REQUEST_KEY);
    }

    @Override
    public void init(FilterConfig arg0) throws ServletException {
    }

    private RequestInfo buildRequestInfo(ServletRequest request) {
        HttpServletRequest req = (HttpServletRequest) request;
        RequestInfo reqInfo = new RequestInfo();
        reqInfo.setSession(req.getSession().getId());
        reqInfo.setRemoteHost(req.getRemoteHost());
        reqInfo.setRequestURI(req.getRequestURI());
        reqInfo.setRequestURL(req.getRequestURL().toString());
        reqInfo.setQueryString(req.getQueryString());
        reqInfo.setUserAgent(req.getHeader("User-Agent"));
        reqInfo.setForwarded(req.getHeader("X-Forwarded-For"));
        reqInfo.setRemoteIp(req.getRemoteAddr());
        reqInfo.setMethod(req.getMethod());
        return reqInfo;
    }

    class RequestInfo {
        private String requestURL;
        private String requestURI;
        private String queryString;
        private String method;
        private String remoteHost;
        private String remoteIp;
        private String session;
        private String userAgent;
        private String Forwarded;

        public String getRequestURL() {
            return requestURL;
        }

        public void setRequestURL(String requestURL) {
            this.requestURL = requestURL;
        }

        public String getRequestURI() {
            return requestURI;
        }

        public void setRequestURI(String requestURI) {
            this.requestURI = requestURI;
        }

        public String getQueryString() {
            return queryString;
        }

        public void setQueryString(String queryString) {
            this.queryString = queryString;
        }

        public String getMethod() {
            return method;
        }

        public void setMethod(String method) {
            this.method = method;
        }

        public String getRemoteHost() {
            return remoteHost;
        }

        public void setRemoteHost(String remoteHost) {
            this.remoteHost = remoteHost;
        }

        public String getRemoteIp() {
            return remoteIp;
        }

        public void setRemoteIp(String remoteIp) {
            this.remoteIp = remoteIp;
        }

        public String getSession() {
            return session;
        }

        public void setSession(String session) {
            this.session = session;
        }

        public String getUserAgent() {
            return userAgent;
        }

        public void setUserAgent(String userAgent) {
            this.userAgent = userAgent;
        }

        public String getForwarded() {
            return Forwarded;
        }

        public void setForwarded(String forwarded) {
            Forwarded = forwarded;
        }
    }
}